package com.ubing.boot.notesforjava.test.NIO;

import org.omg.CORBA.TIMEOUT;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName : sokect_test
 * @Description : sokect_test
 * @Author : Xxxxx
 * @Date: 2023-03-24 15:11
 */
public class sokect_test {
    private static final int RECEIVE_BUFFER = 10;
    private static final int SO_TIMEOUT = 0;
    private static final boolean REUSE_ADDR = false;
    private static final int BACK_LOG = 2;
    private static final boolean CLI_KEEPALIVE = false;
    private static final boolean CLI_OOB = false;
    private static final int CLI_REC_BUF = 20;
    private static final boolean CLI_REUSE_ADDR = false;
    private static final int CLI_SEND_BUF = 20;
    private static final boolean CLI_LINGER = true;
    private static final int CLI_LINGER_N = 0;
    private static final int CLI_TIMEOUT = 0;
    private static final boolean CLI_NO_DELAY = false;


    public static void main(String[] args) {

        ServerSocket server = null;

        try {
            server =new ServerSocket();
            server.bind(new InetSocketAddress(9090),BACK_LOG);
            server.setReceiveBufferSize(RECEIVE_BUFFER);
            server.setSoTimeout(SO_TIMEOUT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("server up use 9090");

        while (true){
            try {
                System.in.read();
                Socket client = server.accept();
                System.out.println("client port:"+ client.getPort());
                client.setKeepAlive(CLI_KEEPALIVE);
                client.setOOBInline(CLI_OOB);
                client.setReceiveBufferSize(CLI_REC_BUF);
                client.setReuseAddress(REUSE_ADDR);
                client.setSoLinger(CLI_LINGER,CLI_LINGER_N);
                client.setSoTimeout(CLI_TIMEOUT);
                client.setTcpNoDelay(CLI_NO_DELAY);

                new Thread(()->{
                    while(true){
                        try {
                            InputStream in = client.getInputStream();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                            char[] data = new char[1024];
                            int num=reader.read(data);
                            if(num>0){
                                System.out.println("clint read some data is:"+num+"val:"+new String(data,0,num));
                            }else if(num==0){
                                System.out.println("clint readed nothing");
                                continue;
                            }else {
                                System.out.println("clint readed -1...");
                                client.close();
                                break;
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                }).start();

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
